package com.kanq.controller;

import com.kanq.tool.R;
import com.kanq.util.SM4Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/blade-ai/encrypt")
public class EncryptController {

    @Value("${decrypt.local.sm4-key}")
    private String sm4Key;

    @GetMapping("/sm4/key")
    public R<String> generateSM4Key() {
        try {
            return R.data(SM4Util.generateKey());
        } catch (Exception e) {
            return R.fail("生成密钥失败: " + e.getMessage());
        }
    }

    @PostMapping("/sm4")
    public R<String> encryptBySM4(@RequestBody String plainText) {
        try {
            return R.data(SM4Util.encrypt(plainText, sm4Key));
        } catch (Exception e) {
            return R.fail("加密失败: " + e.getMessage());
        }
    }
}